package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 */
public class CardPicAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mItems;

    public CardPicAdapter(Context context, List<String> mItems) {
        this.mContext = context;
        this.mItems = mItems;
    }

    public int getCount() {
        return mItems.size();
    }

    public Object getItem(int position) {
        return mItems.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from
                    (mContext).inflate(R.layout.cardpic_item, null, false);
//            holder.item = (RelativeLayout) convertView.findViewById(R.id.cardpic_item_item);
            holder.iv = (SimpleDraweeView) convertView.findViewById(R.id.cardpic_item_iv);
            holder.del = (ImageView) convertView.findViewById(R.id.cardpic_item_cancel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.iv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, getHigh(mContext)));
        final String listItems = mItems.get(position);
        if (listItems.startsWith("http")) {
            ImageOptHelper.showFrescoNative(Uri.parse(listItems), holder.iv, mContext);
        } else {
            ImageOptHelper.showFrescoNative(Uri.parse("file://" + listItems), holder.iv, mContext);
        }

        final ImageView miv = holder.iv;
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                miv.setFocusable(true);
                if (position == 0 && mItems.size() == 1) {
                    Toast.makeText(mContext, "至少要选一张图片哦", Toast.LENGTH_SHORT).show();
                    return;
                }
                mItems.remove(position);
                notifyDataSetChanged();
                for (int i = 0; i < SelectedImg.mSelectedImage.size(); i++) {
                    if (listItems.equals(SelectedImg.mSelectedImage.get(i))) {
                        SelectedImg.mSelectedImage.remove(i);
                    }
                }
                mContext.sendBroadcast(new Intent(ACTIONs.updataMcardPic));
            }
        });
        return convertView;
    }

    private class ViewHolder {
        //        RelativeLayout item;
        SimpleDraweeView iv;
        ImageView del;
    }

    public int getHigh(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        int pwidth = wm.getDefaultDisplay().getWidth();
        int imgwidth = pwidth - DisplayUtils.dp2px(context, 40);
        int singleimgwidth = imgwidth / 2;
        return singleimgwidth * 460 / 326;
    }


}
